package com.zbcz.erp.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.zbcz.erp.adapter.CommonListAdapter
import com.zbcz.erp.adapter.MyApplyAdapter
import com.zbcz.erp.adapter.MyApplyToolsAdapter.Companion.extractAfterSecondLastSlash
import com.zbcz.erp.databinding.ItemCommonListFragmentBinding
import com.zbcz.erp.dialog.CancelDialog
import com.zbcz.erp.util.ToastUtils
import com.zbcz.erp.viewmodel.MainActivityViewModel

class CommonListFragment : Fragment() {

    private lateinit var mBinding: ItemCommonListFragmentBinding

    private val mViewModel: MainActivityViewModel by activityViewModels()

    companion object {

        const val TITLE = "title"
        const val TYPE = "type"
        const val MY_TO_DO_LIST = 100
        const val MY_DONE_LIST = 101
        const val MY_APPLY = 102
        const val RE_APPLY = 103
        const val DONE_JOB_BEAN = "DONE_JOB_BEAN"
        fun createInstance(title: String, type: Int = 0): CommonListFragment {
            return CommonListFragment().apply {
                val args = Bundle()
                args.putString(TITLE, title)
                args.putInt(TYPE, type)
                arguments = args
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        mBinding = ItemCommonListFragmentBinding.inflate(inflater, container, false)
        init()
        return mBinding.root
    }

    private var mAdapter: CommonListAdapter? = null

    private var type: Int = 0

    private fun init() {
        type = arguments?.getInt(TYPE, 0) ?: 0
        mAdapter = CommonListAdapter(type ?: 0)
        mBinding.rcList.adapter = mAdapter
        when (type) {
            MY_TO_DO_LIST -> {
                mAdapter?.onItemClickListener = { position, doneJobBean ->
                    if (doneJobBean.isSuspended) {
                        ToastUtils.show("当前任务已挂起，请联系管理员")
                    } else {
//                        Log.i(TAG, currentList[adapterPosition].toString())
//                        val routeUrl = extractAfterSecondLastSlash(currentList[adapterPosition].routeName)
//                        if (routeUrl != null) {
//                            ARouter.getInstance().build(routeUrl)
//                                .withParcelable(MY_TOOLS_BEAN, currentList[adapterPosition]).navigation()
//                        }
                        val routeUrl = extractAfterSecondLastSlash(doneJobBean.routeName)
                        ARouter.getInstance().build(routeUrl).withInt(TYPE, type)
                            .withParcelable(DONE_JOB_BEAN, doneJobBean).navigation()
                    }
                }
                mViewModel.getToDoList()
                mViewModel.mToDoListJobListLiveData.observe(viewLifecycleOwner) {
                    mBinding.refreshLayout.finishRefresh()
                    mAdapter?.submitList(it)
                }
                mViewModel.mActPassLiveData.observe(viewLifecycleOwner) {
                    if (it.first) {
                        ToastUtils.show(it.second ?: "")
                        mViewModel.getToDoList()
                    } else {
                        ToastUtils.show(it.second ?: "")
                    }
                }
            }

            MY_DONE_LIST -> {
                mViewModel.getDoneList()
                mViewModel.mDoneJobListLiveData.observe(viewLifecycleOwner) {
                    mBinding.refreshLayout.finishRefresh()
                    mAdapter?.submitList(it)
                }
                mAdapter?.onItemClickListener = { position, doneJobBean ->
                    val routeUrl = extractAfterSecondLastSlash(doneJobBean.routeName)
                    ARouter.getInstance().build(routeUrl).withInt(TYPE, type)
                        .withParcelable(DONE_JOB_BEAN, doneJobBean).navigation()
                }
            }

            MY_APPLY -> {
                mViewModel.getMyApplyList()
                mViewModel.mDoingJobListLiveData.observe(viewLifecycleOwner) {
                    mBinding.refreshLayout.finishRefresh()
                    val mAdapter = MyApplyAdapter()
                    mAdapter.submitList(it)
                    mBinding.rcList.adapter = mAdapter
                    mAdapter.onItemClickListener = { position, doneJobBean ->
                        val routeUrl = extractAfterSecondLastSlash(doneJobBean.routeName)
                        ARouter.getInstance().build(routeUrl).withInt(TYPE, type)
                            .withParcelable(DONE_JOB_BEAN, doneJobBean).navigation()
                    }
                    mAdapter.onCancelClickListener = { position, doneJobBean ->
                        showCancelDialog {
                            mViewModel.cancelApply(
                                doneJobBean.id ?: "",
                                doneJobBean.procInstId ?: ""
                            )
                            mCancelDialog?.dismiss()
                        }
                    }
                }
                mViewModel.mCancelLiveData.observe(viewLifecycleOwner) {
                    if (it) {
                        ToastUtils.show("撤回成功")
                        mViewModel.getMyApplyList()
                    } else {
                        ToastUtils.show("撤回失败")
                    }
                }
            }
        }
        mBinding.refreshLayout.run {
            setEnableRefresh(true)
            setEnableLoadMore(false)
            setOnRefreshListener {
                when (type) {
                    MY_TO_DO_LIST -> {
                        mViewModel.getToDoList()
                    }

                    MY_DONE_LIST -> {
                        mViewModel.getDoneList()
                    }

                    MY_APPLY -> {
                        mViewModel.getMyApplyList()
                    }
                }
            }
        }
    }

    private var mCancelDialog: CancelDialog? = null

    private fun showCancelDialog(onConfirmListener: (() -> Unit)?) {
        if (mCancelDialog == null) {
            mCancelDialog = CancelDialog(requireContext())
        }
        mCancelDialog?.onConfirmListener = onConfirmListener
        mCancelDialog?.show()
    }

    override fun onResume() {
        when (type) {
            MY_TO_DO_LIST -> {
                mViewModel.getToDoList()
            }

            MY_DONE_LIST -> {
                mViewModel.getDoneList()
            }

            MY_APPLY -> {
                mViewModel.getMyApplyList()
            }
        }
        super.onResume()
    }
}